﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ColorApplication.BusinessLayer;

namespace ColorApplication.color
{
    public partial class SelectedAttrsToOpts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            grdVwAttribute.RowDataBound += grdVwAttribute_RowDataBound;

            if (Request.QueryString["id"] == null || Request.QueryString["id"] != ConfigurationManager.AppSettings["AdminID"])
            {
                btnSaveTop.Visible = false;
                btnSaveBottom.Visible = false;
            }

            if (!IsPostBack)
            {
                BindDataToGridView();
            }
        }

        private void BindDataToGridView()
        {
            grdVwAttribute.DataSource = ColorSayDaoManager.Instance.GetAttributesFamiliesSelectByTopCount(-1);
            grdVwAttribute.DataBind();
        }

        private void grdVwAttribute_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var dataItem = e.Row.DataItem as ColorSayAttribute;
                if (dataItem == null) return;

                var chkSelectedForOption = e.Row.FindControl("chkSelectedForOption") as CheckBox;
                if (chkSelectedForOption != null)
                {
                    chkSelectedForOption.Checked = dataItem.IsSelectedForOpt;
                }

                var lblAttributeFamily = e.Row.FindControl("lblAttributeFamily") as Label;
                if (lblAttributeFamily != null)
                {
                    lblAttributeFamily.Text = dataItem.Description;
                }

                var hfViewAttribute = e.Row.FindControl("hfViewAttribute") as HiddenField;
                if (hfViewAttribute != null)
                {
                    hfViewAttribute.Value = dataItem.Id.ToString();
                }


            }
        }

        protected void SaveAttributesForOption(object sender, EventArgs e)
        {
            try
            {
                var listAttsId = new List<int>();
                foreach (GridViewRow row in grdVwAttribute.Rows)
                {
                    var chk = row.FindControl("chkSelectedForOption") as CheckBox;
                    if (chk != null && chk.Checked)
                    {
                        var hfViewAttribute = row.FindControl("hfViewAttribute") as HiddenField;
                        if (hfViewAttribute != null)
                        {
                            listAttsId.Add(int.Parse(hfViewAttribute.Value));
                        }
                    }
                }

                if (listAttsId.Count > 0)
                {
                    ColorSayDaoManager.Instance.UpdateSelectAttrsForOption(listAttsId);
                }
                errorMessage.Visible = true;
                errorMessage.Text = "Update successfully.";
            }
            catch (Exception exception)
            {
                errorMessage.Visible = true;
                errorMessage.Text = string.Format("{0}, {1}", exception.Message, exception.StackTrace);
            }
            finally
            {
                BindDataToGridView();
            }
        }
    }
}